// ==UserScript==
// @name           Vkontakte Mail Improver
// @namespace      http://polkila.googlecode.com
// @author         Васютинский Олег
// @version        9.04
// @include        http://vkontakte.ru/mail.php*
// @include        http://www.vkontakte.ru/mail.php*
// @include        http://vkontakte.ru/im.php*
// @include        http://www.vkontakte.ru/im.php*
// ==/UserScript==

var scriptName = 'VkontakteRu.MailImprover';
var scriptVersion = 9.04;
var lang = parseInt(readCookie('remixlang'));

//removeEvent(ge('el'), 'click');
//addEvent(ge('el'), 'click', onClick);

(function(){
	if (!window.ge) window = unsafeWindow; // firefox
	//if (!window.ge) window.ge = function(i){return document.getElementById(i);} // chrome

	var _settings_ = lang?'Show settings':'&#1053;&#1072;&#1089;&#1090;&#1088;&#1086;&#1080;&#1090;&#1100; &#1074;&#1080;&#1076;';
	var _title_ = lang?'Type background color':'&#1059;&#1082;&#1072;&#1078;&#1080;&#1090;&#1077; &#1094;&#1074;&#1077;&#1090; &#1087;&#1086;&#1076;&#1083;&#1086;&#1078;&#1082;&#1080;';
	var _cancel_ = lang?'Cancel':'&#1054;&#1090;&#1084;&#1077;&#1085;&#1072;';
	var _example_ = lang?'For example':'&#1053;&#1072;&#1087;&#1088;&#1080;&#1084;&#1077;&#1088;';
	var _or_ = lang?'or':'&#1080;&#1083;&#1080;';
	var _default_ = 'black';
	var xcolor = readCookie('xcolor') || _default_;
	var showMIDialog = function(){
		if (typeof(box)=='undefined'){
			box = new window.MessageBox({title:_title_});
			box.content('<input type="text" id="xcolor" value="'+xcolor+'"><br><small>'+_example_+': <span class="colEx" style="background-color:black;color:white" onclick="ge(\'xcolor\').value=this.innerHTML">'+_default_+'</span> '+_or_+' <span class="colEx" style="background-color:#fffea4" onclick="ge(\'xcolor\').value=this.innerHTML">#fffea4</span></small>');
			box.addButton({label:_cancel_, style:'button_no', onClick:box.hide});
			box.addButton({label:'OK', onClick:function(){xcolor=window.ge('xcolor').value;createCookie('xcolor',xcolor,365);setColor(xcolor);box.hide();}});
		}
		box.show();
	}
	//window.showMIDialog = showMIDialog; // firefox
	setColor(xcolor);
	setStyle('#boxFader{position:fixed;z-index:10000;display:none}#xcolor{width:97%;margin-bottom:5px}.colEx,#settingsBtn{cursor:pointer;border-bottom:1px dashed}#settingsBtn{margin:0 0 -1px 20px;float:right}');
	document.getElementsByClassName('tBar')[0].childNodes[1].innerHTML+='<div id="settingsBtn" onclick="showMIDialog()">'+_settings_+'</div>';
	$('settingsBtn').addEventListener('click', showMIDialog); // chrome
	addJS('/js/box.js');
	addCSS('/css/box.css');
	if ((xcount=document.getElementsByClassName('newRow').length)>0) document.title = '('+xcount+') '+document.title;
    setTimeout(checkForUpdate, 2000);
})();

function $(i){
	return document.getElementById(i);
}

function setColor(c){
	setStyle('.mailbox .newRow, .im_hist tr.un {background-color: '+c+' !important} .mailbox .messageBody, .im_hist tr.un {color:#2B587A !important}');
}

function setStyle(str){
	node = document.createElement('style');
	node.appendChild(document.createTextNode(str));
	getHead().appendChild(node);
}

function addJS(src){
	node = document.createElement('script');
	node.src = src;
	getHead().appendChild(node);
}

function addCSS(src){
	node = document.createElement('link');
	node.rel = 'stylesheet';
	node.href = src;
	getHead().appendChild(node);
}

function getHead(){
	return document.getElementsByTagName('head')[0];
}

function readCookie(name){
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for (i=0; i<ca.length; i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else expires = '';
	document.cookie = name+'='+value+expires+'; path=/';
}

function checkForUpdate(){
	if (readCookie(scriptName+'.checked')) return;
	GM_xmlhttpRequest({
		method: 'GET',
		url: 'http://polkila.googlecode.com/svn/trunk/'+scriptName+'.ver',
		onload: function(result){
			createCookie(scriptName+'.checked', true, 3);
			newVer = parseFloat(result.responseText);
			if (newVer>scriptVersion && confirm(lang?'There is a new version of '+scriptName+' script.\nDo You want to update?':'Появилась новая версия скрипта '+scriptName+'.\nОбновить?')){
				location.href = 'http://polkila.googlecode.com/files/'+scriptName+'.'+newVer+'.user.js';
			}
		}
	});
}
